<template>
  <!-- 封装的设备类型管理的新增组件 -->
  <div>
    <el-dialog
      title="新增设备类型"
      :visible="visible"
      width="30%"
      @close="$emit('addShowFn')"
    >
      <el-form ref="form" :model="iptValue" :rules="form" label-width="80px">
        <el-form-item label="型号名称:" prop="name" label-width="100px">
          <el-input v-model="iptValue.name" />
        </el-form-item>
        <el-form-item label="型号编码:" prop="model" label-width="100px">
          <el-input v-model="iptValue.model" />
        </el-form-item>
        <el-form-item label="货道行数:" prop="vmRow" label-width="100px">
          <el-input-number
            v-model="iptValue.vmRow"
            style="width:430px"
            controls-position="right"
            :min="1"
            :max="10"
          />
        </el-form-item>
        <el-form-item label="货道列数:" prop="vmCol" label-width="100px">
          <el-input-number
            v-model="iptValue.vmCol"
            style="width:430px"
            controls-position="right"
            :min="1"
            :max="10"
          />
        </el-form-item>
        <el-form-item label="货道容量:" prop="channelMaxCapacity" label-width="100px">
          <el-input-number
            v-model="iptValue.channelMaxCapacity"
            style="width:430px"
            controls-position="right"
            :min="1"
            :max="10"
          />
        </el-form-item>
        <el-form-item label="设备图片:" prop="image" label-width="100px">
          <div
            action="#"
            class="avatar-uploader"
            @click="$refs.iptFile.click()"
          >
            <img v-if="iptValue.image" class="pic" :src="iptValue.image" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <input
              v-show="false"
              ref="iptFile"
              type="file"
              accept="image/*"
              @change="onFileChange"
            >
          </div>
          <!-- 第一次写法 -->
          <!-- <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="iptValue.image" :src="iptValue.image" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('addShowFn')">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { picApi, AddTactics, modifyTactics } from '@/api'
// import { AddTactics } from '@/api'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      row: '',
      iptValue: {
        name: '', // 名称
        model: '', // 编码
        vmRow: '', // 行数
        vmCol: '', // 列数
        channelMaxCapacity: '', // 容量
        image: '' // 图片
      },
      form: {
        name: {
          required: true,
          message: '请输入型号',
          trigger: 'blur'
        },
        model: {
          required: true,
          message: '请输入编码',
          trigger: 'blur'
        },
        vmRow: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        vmCol: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        channelMaxCapacity: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        image: {
          required: true,
          message: '请放入图片',
          trigger: 'blur'
        }
      }
    }
  },
  watch: {
    tow() {
      console.log(this.row)
    }
  },
  methods: {
    modifyFn(row) {
      this.row = row
      this.iptValue = {
        name: row.name, // 名称
        model: row.model, // 编码
        vmRow: row.vmRow, // 行数
        vmCol: row.vmCol, // 列数
        channelMaxCapacity: row.channelMaxCapacity, // 容量
        image: row.image // 图片
      }
    },
    async addSubmit() {
    // 确认
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.row.name) {
          await modifyTactics(this.row.typeId, this.iptValue)
          this.$message.success('修改成功')
          this.$emit('addShowFn')
        } else {
          try {
            await AddTactics(this.iptValue)
            this.$message.success('添加成功')
            this.$emit('addShowFn')
            this.$emit('publicFn')
          } catch (error) {
            this.$message.success('失败')
            console.log(error)
          }
        }
      })
    },
    // 文件选择方法
    async onFileChange(ev) {
      //   console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('fileName', ev.target.files[0]) // photo在表单里参数名携带
      const res = await picApi(fd)
      console.log(res)
      // console.log(res.data.data.photo)
      this.iptValue.image = res.data
      // this.profile.photo =  // 更新最新头像
    }
    // 第一次写法
    // handleAvatarSuccess(res, file) {
    //   console.log(res); console.log(file)
    //   this.iptValue.image = URL.createObjectURL(file.raw)
    //   // console.log(this.imageUrl)
    // },
    // beforeAvatarUpload(file) {
    //   const isJPG = file.type === 'image/jpeg'
    //   const isLt2M = file.size / 1024 / 1024 < 2

    //   if (!isJPG) {
    //     this.$message.error('上传头像图片只能是 JPG 格式!')
    //   }
    //   if (!isLt2M) {
    //     this.$message.error('上传头像图片大小不能超过 2MB!')
    //   }
    //   return isJPG && isLt2M
    // },
    // async addSubmit() {
    // // 确认
    //   await AddTactics(this.iptValue)
    // }
  }

}

</script>

<style lang="scss" scoped>
.cli {
  background-color: #f3f6fb;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.pic{
  width: 76px;
  height: 76px;
}
</style>
